package top.byteio.common.oauth2.config;

import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import top.byteio.common.oauth2.factory.AuthRequestFactory;
import top.byteio.common.oauth2.properties.Oauth2Properties;

/**
 * <p>
 * JustAuth 自动装配类
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2019-07-22 10:52
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({Oauth2Properties.class})
public class Oauth2AutoConfiguration {

    @Bean
    @ConditionalOnProperty(prefix = "jole.common.oauth2", value = "enabled", havingValue = "true", matchIfMissing = true)
    public AuthRequestFactory authRequestFactory(Oauth2Properties properties, AuthStateCache authStateCache) {
        return new AuthRequestFactory(properties, authStateCache);
    }

    @Configuration
    @Import({
            Oauth2StateCacheConfiguration.Default.class,
            Oauth2StateCacheConfiguration.Redis.class,
            Oauth2StateCacheConfiguration.Custom.class})
    protected static class AuthStateCacheAutoConfiguration {
    }
}
